<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>simplematerial.cpp 示例文件 | Qt Quick 5.11</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtquick-index.html">Qt Quick</a></td><td ><a href="qtquick-scenegraph-simplematerial-example.html">Scene Graph - Simple Material</a></td><td >simplematerial.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.1 参考指南</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">simplematerial.cpp 示例文件</h1>
<span class="subtitle">scenegraph/simplematerial/simplematerial.cpp</span>
<!-- $$$scenegraph/simplematerial/simplematerial.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the demonstration applications of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;qguiapplication.h&gt;</span>

  <span class="preprocessor">#include &lt;qsgmaterial.h&gt;</span>
  <span class="preprocessor">#include &lt;qsgnode.h&gt;</span>

  <span class="preprocessor">#include &lt;qquickitem.h&gt;</span>
  <span class="preprocessor">#include &lt;qquickview.h&gt;</span>

  <span class="preprocessor">#include &lt;qsgsimplerectnode.h&gt;</span>

  <span class="preprocessor">#include &lt;qsgsimplematerial.h&gt;</span>

  <span class="keyword">struct</span> State
  {
      <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color;

      <span class="type">int</span> compare(<span class="keyword">const</span> State <span class="operator">*</span>other) <span class="keyword">const</span> {
          <span class="type"><a href="../qtcore/qtglobal.html#uint-typedef">uint</a></span> rgb <span class="operator">=</span> color<span class="operator">.</span>rgba();
          <span class="type"><a href="../qtcore/qtglobal.html#uint-typedef">uint</a></span> otherRgb <span class="operator">=</span> other<span class="operator">-</span><span class="operator">&gt;</span>color<span class="operator">.</span>rgba();

          <span class="keyword">if</span> (rgb <span class="operator">=</span><span class="operator">=</span> otherRgb) {
              <span class="keyword">return</span> <span class="number">0</span>;
          } <span class="keyword">else</span> <span class="keyword">if</span> (rgb <span class="operator">&lt;</span> otherRgb) {
              <span class="keyword">return</span> <span class="operator">-</span><span class="number">1</span>;
          } <span class="keyword">else</span> {
              <span class="keyword">return</span> <span class="number">1</span>;
          }
      }
  };

  <span class="keyword">class</span> Shader : <span class="keyword">public</span> <span class="type"><a href="qsgsimplematerialshader.html">QSGSimpleMaterialShader</a></span><span class="operator">&lt;</span>State<span class="operator">&gt;</span>
  {
      QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader<span class="operator">,</span> State);
  <span class="keyword">public</span>:

      <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>vertexShader() <span class="keyword">const</span> override {
          <span class="keyword">return</span>
                  <span class="string">&quot;attribute highp vec4 aVertex;                              \n&quot;</span>
                  <span class="string">&quot;attribute highp vec2 aTexCoord;                            \n&quot;</span>
                  <span class="string">&quot;uniform highp mat4 qt_Matrix;                              \n&quot;</span>
                  <span class="string">&quot;varying highp vec2 texCoord;                               \n&quot;</span>
                  <span class="string">&quot;void main() {                                              \n&quot;</span>
                  <span class="string">&quot;    gl_Position = qt_Matrix * aVertex;                     \n&quot;</span>
                  <span class="string">&quot;    texCoord = aTexCoord;                                  \n&quot;</span>
                  <span class="string">&quot;}&quot;</span>;
      }

      <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>fragmentShader() <span class="keyword">const</span> override {
          <span class="keyword">return</span>
                  <span class="string">&quot;uniform lowp float qt_Opacity;                             \n&quot;</span>
                  <span class="string">&quot;uniform lowp vec4 color;                                   \n&quot;</span>
                  <span class="string">&quot;varying highp vec2 texCoord;                               \n&quot;</span>
                  <span class="string">&quot;void main ()                                               \n&quot;</span>
                  <span class="string">&quot;{                                                          \n&quot;</span>
                  <span class="string">&quot;    gl_FragColor = texCoord.y * texCoord.x * color * qt_Opacity;  \n&quot;</span>
                  <span class="string">&quot;}&quot;</span>;
      }
      <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">&gt;</span> attributes() <span class="keyword">const</span> override
      {
          <span class="keyword">return</span> <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">&gt;</span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;aVertex&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;aTexCoord&quot;</span>;
      }
      <span class="type">void</span> updateState(<span class="keyword">const</span> State <span class="operator">*</span>state<span class="operator">,</span> <span class="keyword">const</span> State <span class="operator">*</span>) override
      {
          program()<span class="operator">-</span><span class="operator">&gt;</span>setUniformValue(id_color<span class="operator">,</span> state<span class="operator">-</span><span class="operator">&gt;</span>color);
      }
      <span class="type">void</span> resolveUniforms() override
      {
          id_color <span class="operator">=</span> program()<span class="operator">-</span><span class="operator">&gt;</span>uniformLocation(<span class="string">&quot;color&quot;</span>);
      }

  <span class="keyword">private</span>:
      <span class="type">int</span> id_color;
  };

  <span class="keyword">class</span> ColorNode : <span class="keyword">public</span> <span class="type"><a href="qsggeometrynode.html">QSGGeometryNode</a></span>
  {
  <span class="keyword">public</span>:
      ColorNode()
          : m_geometry(<span class="type"><a href="qsggeometry.html">QSGGeometry</a></span><span class="operator">::</span>defaultAttributes_TexturedPoint2D()<span class="operator">,</span> <span class="number">4</span>)
      {
          setGeometry(<span class="operator">&amp;</span>m_geometry);

          <span class="type"><a href="qsgsimplematerial.html">QSGSimpleMaterial</a></span><span class="operator">&lt;</span>State<span class="operator">&gt;</span> <span class="operator">*</span>material <span class="operator">=</span> Shader<span class="operator">::</span>createMaterial();
          material<span class="operator">-</span><span class="operator">&gt;</span>setFlag(<span class="type"><a href="qsgmaterial.html">QSGMaterial</a></span><span class="operator">::</span>Blending);
          setMaterial(material);
          setFlag(OwnsMaterial);
      }

      <span class="type"><a href="qsggeometry.html">QSGGeometry</a></span> m_geometry;
  };

  <span class="keyword">class</span> Item : <span class="keyword">public</span> <span class="type"><a href="qquickitem.html">QQuickItem</a></span>
  {
      Q_OBJECT

      Q_PROPERTY(<span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color READ color WRITE setColor NOTIFY colorChanged)

  <span class="keyword">public</span>:

      Item()
      {
          setFlag(ItemHasContents<span class="operator">,</span> <span class="keyword">true</span>);
      }

      <span class="type">void</span> setColor(<span class="keyword">const</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> <span class="operator">&amp;</span>color) {
          <span class="keyword">if</span> (m_color <span class="operator">!</span><span class="operator">=</span> color) {
              m_color <span class="operator">=</span> color;
              <span class="keyword">emit</span> colorChanged();
              update();
          }
      }
      <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color() <span class="keyword">const</span> {
          <span class="keyword">return</span> m_color;
      }

  <span class="keyword">信号</span>:
      <span class="type">void</span> colorChanged();

  <span class="keyword">private</span>:
    <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> m_color;

  <span class="keyword">public</span>:
      <span class="type"><a href="qsgnode.html">QSGNode</a></span> <span class="operator">*</span>updatePaintNode(<span class="type"><a href="qsgnode.html">QSGNode</a></span> <span class="operator">*</span>node<span class="operator">,</span> UpdatePaintNodeData <span class="operator">*</span>) override
      {
          ColorNode <span class="operator">*</span>n <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span>ColorNode <span class="operator">*</span><span class="operator">&gt;</span>(node);
          <span class="keyword">if</span> (<span class="operator">!</span>node)
              n <span class="operator">=</span> <span class="keyword">new</span> ColorNode();

          <span class="type"><a href="qsggeometry.html">QSGGeometry</a></span><span class="operator">::</span>updateTexturedRectGeometry(n<span class="operator">-</span><span class="operator">&gt;</span>geometry()<span class="operator">,</span> boundingRect()<span class="operator">,</span> <span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>));
          <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qsgsimplematerial.html">QSGSimpleMaterial</a></span><span class="operator">&lt;</span>State<span class="operator">&gt;</span><span class="operator">*</span><span class="operator">&gt;</span>(n<span class="operator">-</span><span class="operator">&gt;</span>material())<span class="operator">-</span><span class="operator">&gt;</span>state()<span class="operator">-</span><span class="operator">&gt;</span>color <span class="operator">=</span> m_color;

          n<span class="operator">-</span><span class="operator">&gt;</span>markDirty(<span class="type"><a href="qsgnode.html">QSGNode</a></span><span class="operator">::</span>DirtyGeometry <span class="operator">|</span> <span class="type"><a href="qsgnode.html">QSGNode</a></span><span class="operator">::</span>DirtyMaterial);

          <span class="keyword">return</span> n;
      }
  };
  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>argv)
  {
      <span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span> app(argc<span class="operator">,</span> argv);

      qmlRegisterType<span class="operator">&lt;</span>Item<span class="operator">&gt;</span>(<span class="string">&quot;SimpleMaterial&quot;</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">&quot;SimpleMaterialItem&quot;</span>);

      <span class="type"><a href="qquickview.html">QQuickView</a></span> view;
      view<span class="operator">.</span>setResizeMode(<span class="type"><a href="qquickview.html">QQuickView</a></span><span class="operator">::</span>SizeRootObjectToView);
      view<span class="operator">.</span>setSource(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span>(<span class="string">&quot;qrc:///scenegraph/simplematerial/main.qml&quot;</span>));
      view<span class="operator">.</span>show();

      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

  <span class="preprocessor">#include &quot;simplematerial.moc&quot;</span>

</pre>
</div>
<!-- @@@scenegraph/simplematerial/simplematerial.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
